import 'package:bigegg/entity/articel/xmm_article_info.dart';
import 'package:bigegg/entity/user/xmm_wall_user.dart';
import 'package:bigegg/global/user/xmm_account_manager.dart';
import 'package:bigegg/utils/file/xmm_file_util.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../../common/get/controller/xmm_base_controller.dart';
import '../../../global/xmm_storage_manager.dart';

class XmmArticlePublishController extends XmmBaseGetxController {
  void postArticle(String title, String content, List<String> imageList) async {
    try {
      EasyLoading.show();
      await Future.delayed(const Duration(seconds: 2));
      var dir = XmmStorageManager.temporaryDirectory?.path;
      var userId = XmmAccountManager.instance.user.userId;
      var pathList = <String>[];
      for (var image in imageList) {
        String name = image.split('/').last;
        String destPath = '$dir/article/$userId/$name';
        pathList.add(destPath);
        await XmmFileUtil.copyFile(image, destPath);
      }
      var user = XmmAccountManager.instance.user;
      var wallUser = XmmWallUser(
        author: user.userId,
        userId: user.userId,
        nickname: user.nickname,
        avatar: user.avatarUrl,
      );
      var articleInfo = XmmArticleInfo(
        author: user.userId,
        title: title,
        description: content,
        thumbnail: pathList.first,
        followed: 0,
        count: 0,
        isLikeByMe: false,
        content: pathList,
        commentList: [],
        wallUser: wallUser,
      );
      XmmAccountManager.instance.addArticleByMy(articleInfo);
      Get.back();
    } finally {
      EasyLoading.dismiss();
    }
  }
}
